import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'element-plus/theme-chalk/dark/css-vars.css'
import { createI18n } from 'vue-i18n'
import App from './App.vue'
import router from './router'
import { useUserStore } from '@/stores/user'
import ErrorHandler from '@/utils/errorHandler'

const i18n = createI18n({
  legacy: false,
})

const app = createApp(App)
const pinia = createPinia()

// 全局错误处理
app.config.errorHandler = (err, vm, info) => {
  console.error('Vue全局错误:', err);
  ErrorHandler.handleError(err);
};

// 未捕获的Promise错误
window.addEventListener('unhandledrejection', event => {
  console.error('未处理的Promise拒绝:', event.reason);
  ErrorHandler.handleError(event.reason);
});

app.use(pinia)
app.use(router)
app.use(i18n)
app.use(ElementPlus, {
  size: 'default',
  zIndex: 3000,
})

// 恢复登录状态
const userStore = useUserStore()
userStore.restoreAuth()

app.mount('#app')